package com.xysd.express.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.activity.mine.CustomerActivity;
import com.xysd.express.activity.mine.IdentificationActivity;
import com.xysd.express.activity.mine.MessageActivity;
import com.xysd.express.activity.mine.UserInfoActivity;
import com.xysd.express.activity.mine.account.AccountActivity;
import com.xysd.express.activity.mine.setting.SettingActivity;
import com.xysd.express.base.BaseFragment;
import com.xysd.express.bean.User;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * 个人中心
 */
@ContentView(R.layout.fragment_mine)
public class MineFragment extends BaseFragment implements View.OnClickListener {

    @ViewInject(R.id.message_img)
    private ImageView message_img;
    @ViewInject(R.id.head_img)
    private ImageView head_img;
    @ViewInject(R.id.nickName_tv)
    private TextView nickName_tv;
    @ViewInject(R.id.tel_tv)
    private TextView tel_tv;
    @ViewInject(R.id.type_tv)
    private TextView type_tv;
    @ViewInject(R.id.info_layout)
    private LinearLayout info_layout;
    @ViewInject(R.id.account_layout)
    private LinearLayout account_layout;
    @ViewInject(R.id.customer_layout)
    private LinearLayout customer_layout;
    @ViewInject(R.id.setting_layout)
    private LinearLayout setting_layout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return org.xutils.x.view().inject(this,inflater,container);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }


    //重写fragment是否对用户可视，当可视时加载数据（解决viewpager的预加载问题）
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();
    }

    private void isCanLoadData() {
        if (getUserVisibleHint()) {
            if (User.getInstance().isLogin)
                getUserInfo();
        }
    }

    private void init() {
        message_img.setOnClickListener(this);
        type_tv.setOnClickListener(this);
        head_img.setOnClickListener(this);
        info_layout.setOnClickListener(this);
        account_layout.setOnClickListener(this);
        customer_layout.setOnClickListener(this);
        setting_layout.setOnClickListener(this);

    }

    private void getUserInfo() {
        HttpUtil.getUserInfo(new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("获取用户信息",result);
                String code= JSONParser.getStringFromJsonString("Status",result);
                String msg= JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    String Data= JSONParser.getStringFromJsonString("Data",result);
                    User user=new Gson().fromJson(Data,User.class);
                    user.AuthKey=User.getInstance().AuthKey;
                    user.Timestamp=User.getInstance().Timestamp;
                    user.UserId=User.getInstance().UserId;
                    User.getInstance().setUser(user);
                    User.getInstance().isLogin=true;
                    setView();
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("获取用户信息",error);
            }
        });
    }

    private void setView() {
        Glide.with(mActivity).load(User.getInstance().Avatar).into(head_img);
        nickName_tv.setText(User.getInstance().NickName);
        tel_tv.setText(User.getInstance().Mobile);
        if (User.getInstance().IsAuthen){
            type_tv.setText("认证状态[已认证]");
        }else {
            type_tv.setText("认证状态[未认证]");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message_img:
                startAty(MessageActivity.class);
                break;
            case R.id.type_tv:
                startAty(IdentificationActivity.class);
                break;
            case R.id.head_img:
                startAty(UserInfoActivity.class);
                break;
            case R.id.info_layout:
                startAty(UserInfoActivity.class);
                break;
            case R.id.account_layout:
                startAty(AccountActivity.class);
                break;
            case R.id.customer_layout:
                startAty(CustomerActivity.class);
                break;
            case R.id.setting_layout:
                startAty(SettingActivity.class);
                break;
        }
    }
}
